package com.kpx.app.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSON;
import com.kpx.app.R;
import com.kpx.app.base.ActivityBase;
import com.kpx.app.bean.LoginUser;
import com.kpx.app.bean.TagBean;
import com.kpx.app.bean.User;
import com.kpx.app.http.HttpDataApi;
import com.kpx.app.http.HttpDataLoader;
import com.kpx.app.http.HttpDataReqUrl;
import com.kpx.app.http.HttpDataRequest;
import com.kpx.app.http.ResObj;
import com.kpx.app.utils.MyHashMap;
import com.kpx.app.utils.UIUtils;
import com.kpx.app.utils.UserInfoUtils;
import com.kpx.app.utils.uiutils.ToastUtil;
import com.kpx.app.view.TitleView;
import com.kpx.app.view.ViewSign;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/4/11.
 */

public class ActivityUserinfoSign extends ActivityBase {
    @BindView(R.id.titleview)
    TitleView titleview;
    @BindView(R.id.ll_container)
    LinearLayout llContainer;
    private ArrayList<TagBean> tags;

    @Override
    public void initview(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_sign);
        ButterKnife.bind(this);
        UIUtils.setNormalTitle(titleview, "设置标签");
        titleview.setRight("保存", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                save();
            }
        }, R.color.white);
    }

    private void save() {
        showLoadingDialog();
        MyHashMap map2 = new MyHashMap();
        HashMap<String, String> map = new HashMap<String, String>();
        String signs = "";
        for (int i = 0; i < tags.size(); i++) {
            String key = tags.get(i).getDmId() + "";
            String value = "";
            ArrayList<TagBean.ChildTagsBean> list = (ArrayList<TagBean.ChildTagsBean>) tags.get(i).getChildTags();
            for (int j = 0; j < list.size(); j++) {
                if (list.get(j).isSelect()) {
                    value += list.get(j).getDmId() + "&";
                    signs += list.get(j).getTagName() + ",";
                }
            }
            if (!TextUtils.isEmpty(value)) {
                map.put(key, value.substring(0, value.length() - 1));
            }
        }
        if (!TextUtils.isEmpty(signs)) {
            signs = signs.substring(0, signs.length() - 1);
        }
        map2.put("tagsStr", map.toString());
        showLoadingDialog();
        final String finalSigns = signs;
        UserInfoUtils.toUpdateData(ActivityUserinfoSign.this, map2, new UserInfoUtils.OnUpdateUserInfoBackListener() {
            @Override
            public void back(boolean issuccess, String msg) {
                dissmissLoadingDialog();
                if (issuccess) {
                    LoginUser loginUser = UserInfoUtils.getCachedLoginUser();
                    if (loginUser != null) {
                        User user = loginUser.getUSER();
                        if (user != null) {
                            user.setUserselectedTags(finalSigns);
                        }
                    }
                    UserInfoUtils.saveLoginUser(loginUser);
                    finish();
                    ToastUtil.getInstance().showToast(ActivityUserinfoSign.this, "保存成功");
                }
            }
        });
    }

    @Override
    public void initData() {
        showLoadingDialog();
        MyHashMap map = new MyHashMap();
        map.put("pageIndex", "1");
        new HttpDataLoader(new HttpDataApi.OnRequestCallback() {
            @Override
            public void onRequestSuccess(String url, ResObj result, boolean isFrmCache) {
                dissmissLoadingDialog();
                if (result.getCode() == 0) {
                    tags = (ArrayList<TagBean>) result.getData();
                    if (tags != null && tags.size() > 0) {
                        for (int i = 0; i < tags.size(); i++) {
                            ViewSign sign = new ViewSign(ActivityUserinfoSign.this);
                            sign.setData(tags.get(i));
                            llContainer.addView(sign);
                        }
                    }
                } else {
                    ToastUtil.getInstance().showToast(ActivityUserinfoSign.this, result.getMsg());
                }
            }

            @Override
            public void onRequestError(String url, int statusCode, String error) {
                dissmissLoadingDialog();
                ToastUtil.getInstance().showToast(ActivityUserinfoSign.this, error);
            }

        }, TagBean.class, HttpDataRequest.TYPE_LIST).POST(HttpDataReqUrl.URL_POST_GET_TAG_LIST, true, map);
    }

    @Override
    public void onReceiver(Context context, Intent intent) {

    }

}
